#pragma once
//#include<iostream>
#include"comm.hpp"

class lin
{
public:
    lin()
    {
        umask(0);
        int n = ::mkfifo(pipefile.c_str(),gmode);
        if(n < 0)
        {
            std::cerr << "mkfifo" << std::endl;
            return;
        }
        std::cout << "创建文件成功"  << std::endl;
        sleep(10);
    }
    ~lin()
    {
        int n = ::unlink(pipefile.c_str());
        if(n < 0)
        {
            std::cerr << "unlink失败" << std::endl;
            return;
        }
        std::cout << "删除fifo文件成功"  << std::endl;
    }
private:

};

lin init;//可以把上面的创建fifo定义一个全局变量

class server
{
public:
    server():_fp(-1)
    {}

    bool openpipe()//打开文件
    {
        _fp = open(pipefile.c_str(),O_RDONLY);//打开文件
        if(_fp < 0)
        {
            std::cerr << "打开文件失败" << std::endl;
            return false;
        }
        std::cout << "打开文件成功" << std::endl;
        return true;
    }
    int rpipe(std::string* out)//读字符串
    {
        char buff[gsize];
        ssize_t n = ::read(_fp,buff,sizeof(buff)-1);
        if(n > 0)
        {
            buff[n] = 0;//将读到的字符串后面加个\0
            *out = buff;
        }
        return n;
    }
    void closepipe()//关闭文件
    {
        if(_fp > 0) ::close(_fp);
    }
    ~server()
    {

    }
private:
    int _fp; 
};